package com.tanhua.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.*;
import com.tanhua.domain.vo.ErrorResult;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVO;
import com.tanhua.domain.vo.UserInfoVO;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.template.BaiDuAipTemplate;
import com.tanhua.template.OssTemplate;
import com.tanhua.util.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

@Service
@Slf4j
public class UserInfoService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private OssTemplate ossTemplate;
    @Autowired
    private BaiDuAipTemplate baiDuAipTemplate;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private QuestionApi questionApi;
    @Reference
    private SettingsApi settingsApi;
    @Reference
    private BlackListApi blackListApi;

    private static final String TOKEN_PREFIX = "TOKEN_";




    /**
     * 保存新用户信息
     * */
    public ResponseEntity save(UserInfo userInfo) {
        userInfoApi.save(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 保存头像
     *
     */

    public ResponseEntity saveHeader(MultipartFile headPhoto, Long id) throws IOException {

        Boolean is = baiDuAipTemplate.HeaderAip(headPhoto.getBytes());
        if (!is){
            return ResponseEntity.status(500).body(ErrorResult.faceDetectFailed());
        }
        String url = ossTemplate.upHeaderPhoto(headPhoto.getInputStream(), headPhoto.getOriginalFilename());
        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        userInfo.setAvatar(url);
        userInfoApi.updateHead(userInfo);

        return ResponseEntity.ok(null);
    }

    /**
     * 用户资料回显
     */

    public ResponseEntity findUserInfo(Long id) {
        UserInfoVO userInfoVO = new UserInfoVO();
        UserInfo userInfo = userInfoApi.findUserInfo(id);
        BeanUtils.copyProperties(userInfo,userInfoVO );
        return ResponseEntity.ok(userInfoVO);
    }

    /**
     * 更新用户资料
     */
    public ResponseEntity updateInfo(UserInfo userInfo) {

        userInfoApi.updateInfo(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 读取用户设置
     * @return
     */

    public ResponseEntity findSettings() {
        User user = UserHolder.getUser();
        Question question = questionApi.findQuestion(user.getId());
        Settings settings = settingsApi.findSettings(user.getId());
        SettingsVO settingsVO = new SettingsVO();
        settingsVO.setId(user.getId());
        settingsVO.setPhone(user.getMobile());

        if (question!=null){
            settingsVO.setStrangerQuestion(question.getTxt());
        }
        if (settings!=null){
            settingsVO.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVO.setLikeNotification(settings.getLikeNotification());
            settingsVO.setPinglunNotification(settings.getPinglunNotification());
        }
        log.debug("user:"+settingsVO.toString());
        return ResponseEntity.ok(settingsVO);
    }

    public ResponseEntity updateQuestions(String content) {
        User user = UserHolder.getUser();
        Question question = questionApi.findQuestion(user.getId());
        if (question==null){
             question = new Question();
             question.setUserId(user.getId());
             question.setTxt(content);
            questionApi.addQuestions(question);
            
        }else{
            question.setTxt(content);
            questionApi.updateQuestions(question);
        }
        return null;
    }

    public ResponseEntity updateSetting(Settings settings) {
        User user = UserHolder.getUser();
        Settings nowSettings = settingsApi.findSettings(user.getId());
        if (nowSettings==null){
            nowSettings = new Settings();
            BeanUtils.copyProperties(settings,nowSettings );
            nowSettings.setUserId(user.getId());
            settingsApi.addSetting(nowSettings);
        }else {
            nowSettings.setGonggaoNotification(settings.getGonggaoNotification());
            nowSettings.setLikeNotification(settings.getLikeNotification());
            nowSettings.setPinglunNotification(settings.getPinglunNotification());
            settingsApi.updateSetting(nowSettings);
        }
        return null;
    }

    public ResponseEntity blackList(Integer page,Integer pagesize) {
        User user = UserHolder.getUser();
        IPage<UserInfo> ipage = blackListApi.blackList(user.getId(),page,pagesize);
        PageResult<UserInfo> pageResult = new PageResult<>();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        pageResult.setPages((int)ipage.getPages());
        pageResult.setCounts((int)ipage.getTotal());
        pageResult.setItems(ipage.getRecords());

        return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity deleteBlackList(Integer blackUserId) {
        blackListApi.deleteBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }
}
